package com.kgc.cgprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.cgapi.pojo.*;
import com.kgc.cgprovider.dao.CGDDDao;
import com.kgc.cgprovider.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
@Transactional
public class CGDDServiceImpl implements CGDDService {

    private Logger logger = LoggerFactory.getLogger(CGDDServiceImpl.class);

    @Resource
    private CGDDDao cgddDao;

    @Resource
    private CGServ cgServ;

    @Resource
    private CGXJService cgxjServ;

    @Resource
    private GYBJService gybjServ;

    @Resource
    private CGBJService cgbjServ;




    @Override
    public ResponseMsg getCGDDPage(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        int pageNow = 0;
        int pageSize = 0;
        if(map.get("pageNum") instanceof String){
            pageNow =Integer.parseInt((String) map.get("pageNum"));
            pageSize =Integer.parseInt((String) map.get("pageSize"));
        }else {
            pageNow = (Integer) map.get("pageNum");
            pageSize = (Integer) map.get("pageSize");
        }

        PageHelper.startPage(pageNow,pageSize);
        List<CGDDPojo> list = cgddDao.getCGDDPage(map);
        PageInfo<CGDDPojo> pageInfo = new PageInfo<>(list);
        responseMsg.setCode(200);
        responseMsg.setData(pageInfo);
        return responseMsg;
    }

    @Override
    public ResponseMsg addCGDD(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        ArrayList<Map<String, Object>> arr =(ArrayList<Map<String, Object>>) map.get("list");
        String code ="CGDD"+"-"+ cgServ.getCode("cg_ecgdd");
        map.put("code",code);
        Date date = new Date();
        map.put("create_date",date);
        int result = cgddDao.addCGDD(map);
        logger.info("addCGDD  + map {} result:{}",map,result);
        if (result <= 0){
            return responseMsg;
        }
        for (Map<String,Object> info : arr){
            info.put("major_code",code);
            info.put("create_date",date);
            info.put("create_id",map.get("create_id"));
            result = cgddDao.addCGDDInfo(info);
            logger.info("addCGDDInfo  + info {} result:{}",info,result);
            if (result <= 0){
                return responseMsg;
            }
        }
        responseMsg.setData(code);
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg getCGDDByCode(String code) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        CGDDPojo cgddPojo = cgddDao.getCGDD(code);
        logger.info("getCGDDByCode code{} , cgddPojo:{}",cgddPojo ,code);
        List<CGDDInfo> list = cgddDao.getCGDDInfo(code);
        cgddPojo.setList(list);
        responseMsg.setCode(200);
        responseMsg.setData(cgddPojo);
        return responseMsg;
    }

    @Override
    public ResponseMsg modiCGDD(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        ArrayList<Map<String, Object>> arr =(ArrayList<Map<String, Object>>) map.get("list");
        Object uid = map.get("uid");
        Date date = new Date();
        map.put("modi_date",date);
        map.put("modi_id",uid);
        int result = cgddDao.modiCGDD(map);
        logger.info("modiCGDD  + map {} result:{}",map,result);
        if (result <= 0){
            return responseMsg;
        }
        String code = (String) map.get("code");
        result = cgddDao.delCGDDInfo(code);
        logger.info("delCGDD  + map {} result:{}",map,result);
        for (Map<String,Object> info : arr){
            if(   info.get("major_code") == null ||  info.get("major_code").toString().equals("")){
                info.put("major_code",code);
                info.put("create_date",date);
                info.put("create_id",map.get("modi_id"));
            }else {
                info.put("modi_id",map.get("modi_id"));
                info.put("modi_date",date);
            }
            result = cgddDao.addCGDDInfo(info);
            if(result <= 0){
                return responseMsg;
            }
        }
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg passCGDD( String code) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(200);
        CGDDPojo cgddPojo = cgddDao.getCGDD(code);
        String cgbjCode =cgddPojo.getCgbj_code();
        if (cgbjCode == null || cgbjCode.equals("")){

        }else {
            CGBJPojo cgbjPojo =(CGBJPojo) cgbjServ.getCGBJ(cgbjCode).getData();
            String cgxjCode = cgbjPojo.getCgxj_code();
            CGXJPojo cgxjPojo = (CGXJPojo) cgxjServ.getCGXJPojo(cgxjCode).getData();
            String cgsqCode = cgxjPojo.getCgsq_code();
            boolean result = false;

            result = cgServ.closeOrder("cg_acgsq" , cgsqCode);
            logger.info("close CGSQ code {} result{} ",cgsqCode,result);
            if(!result){responseMsg.setCode(500);}

            result = cgServ.closeOrder("cg_bcgxj" , cgxjCode);
            logger.info("close CGXJ code {} result{} ",cgxjCode,result);
            if(!result){responseMsg.setCode(500);}

            ResponseMsg back = new ResponseMsg();
            back = gybjServ.closeGYBJByCGXJ(cgxjCode);
            logger.info("close GYBJ code {} result{} ",cgxjCode,back);
            responseMsg.setCode(back.getCode());

            back = cgbjServ.closeCGBJByCGXJ(cgxjCode);
            logger.info("close CGBJ code {} result{} ",cgxjCode,back);
            responseMsg.setCode(back.getCode());
            responseMsg.setCode(back.getCode());
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg updateCGDDStatus(String code, String status) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(200);
        Map<String,Object> map = new HashMap<>();
        map.put("status",status);
        map.put("code",code);
        int result = cgddDao.updateCGDDStatus(map);
        logger.info("updateCGDDStatus code {} result{} ",code,result);
        if (result > 0 ){
            responseMsg.setCode(200);
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg getCGDDByProId(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        int pageNow = 0;
        int pageSize = 0;
        if(map.get("pageNum") instanceof String){
            pageNow =Integer.parseInt((String) map.get("pageNum"));
            pageSize =Integer.parseInt((String) map.get("pageSize"));
        }else {
            pageNow = (Integer) map.get("pageNum");
            pageSize = (Integer) map.get("pageSize");
        }
        PageHelper.startPage(pageNow,pageSize);
        List<CGDDPojo> cgbjList = cgddDao.getCGDDByProId(map);
        for (CGDDPojo cgdd : cgbjList){
            String code = cgdd.getCode();
            List<CGDDInfo> infoList = cgddDao.getCGDDInfo(code);
            cgdd.setList(infoList);
        }
        PageInfo<CGDDPojo> pageInfo = new PageInfo<>(cgbjList);
        responseMsg.setCode(200);
        responseMsg.setData(pageInfo);
        return responseMsg;

    }

    @Override
    public ResponseMsg updateCGDDData(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        int result = cgddDao.updateCGDDData(map);
        logger.info("updateCGDDData code {} result{} ",map.get("code"),result);
        if (result <= 0 ){
            return responseMsg;
        }

        CGDDPojo cgddPojo = cgddDao.getCGDD(map.get("code").toString());
        int compare = cgddPojo.getTotal().compareTo(cgddPojo.getFinish_amount().add(cgddPojo.getSettle_amount()));
        if (compare < 0 || compare ==0  ){
            Map<String,Object> params = new HashMap<>();
            params.put("code",cgddPojo.getCode());
            params.put("status","11");
            cgddDao.updateCGDDStatus(params);
        }
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg getPrePayRefundAmount(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        CGDDPojo cgddPojo = cgddDao.getCGDD(map.get("code").toString());
        BigDecimal maxAmount = cgddPojo.getAdv_amount().subtract( cgddPojo.getUse_amount());
        responseMsg.setData(maxAmount);
        responseMsg.setCode(200);
        return responseMsg;
    }
}
